package cn.yuxiaoyuan.system.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.yuxiaoyuan.base.service.impl.BaseServiceImpl;
import cn.yuxiaoyuan.system.domain.Employee;
import cn.yuxiaoyuan.system.domain.Logininfo;
import cn.yuxiaoyuan.system.domain.Menu;
import cn.yuxiaoyuan.system.domain.Permission;
import cn.yuxiaoyuan.system.mapper.LogininfoMapper;
import cn.yuxiaoyuan.system.mapper.MenuMapper;
import cn.yuxiaoyuan.system.service.LogininfoService;
import cn.yuxiaoyuan.system.vo.LoginVo;
import cn.yuxiaoyuan.util.JwtUtil;
import cn.yuxiaoyuan.util.Payload;
import cn.yuxiaoyuan.utils.R;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LogininfoServiceImpl extends BaseServiceImpl<Logininfo> implements LogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public R login(LoginVo loginVo) {
        if(StringUtils.isEmpty(loginVo.getUsername())){
            return R.fail("用户名为空");
        }
        if(StringUtils.isEmpty(loginVo.getPassword())){
            return R.fail("密码为空");
        }
        //根据账号名和type一起查询数据  -- logininfo
        Logininfo logininfo = logininfoMapper.findLogininfoByUsernameAndType(loginVo);
        if(logininfo == null){
            return R.fail("账号或密码错误");
        }
        //通过数据的密码 和 查询出来的盐值加密
        String saltPwd = logininfo.getSalt() + loginVo.getPassword();
        String newPwd = SecureUtil.md5(saltPwd);
        //判断和数据库的密码是否一致
        if(!logininfo.getPassword().equals(newPwd)){
            //不一致  报错
            return R.fail("账号或密码错误");
        }
        //一致 生成{jwt}token[对应的菜单 对应的权限 对应用户信息] 返回数据给前端
        Payload payload = new Payload();
        payload.setLogininfo(logininfo);
        String token = JwtUtil.generateToken(payload);
        //[对应用户信息 对应的菜单 对应的权限] 根据logininfo去找 只有管理员才有菜单和权限
        Map<String,Object> result = new HashMap<>();
        result.put("token",token);
        if(logininfo.getType() == 0){
            //有logininfo_id
            Long id = logininfo.getId();
            //有employee_id
            Employee employee = logininfoMapper.findEmpByLoginInfoId(id);
            payload.setEmployee(employee);
            result.put("userinfo",employee);
            //permission_id  只要sn
            List<String> permissions = logininfoMapper.findPerByEmpId(employee.getId());
            result.put("permissions",permissions);
            payload.setPermissions(permissions);
            //menu_id
            List<Menu> menus = logininfoMapper.findMenuByEmpId(employee.getId());
            //差父级
            List<Long> menupid = menus.stream().map(Menu::getParentId).distinct().collect(Collectors.toList());
            List<Menu> menusP = menuMapper.findIds(menupid);
            menus.addAll(menusP);
            //直接返回list给前端不得行
            //封装成一颗树
            List<Menu> menusTree = menusTree(menus);
            payload.setMenus(menusTree);
            result.put("menus",menusTree);
        }
        return R.ok(result);
    }

    public List<Menu> menusTree(List<Menu> menus){
        if(menus == null){
            return null;
        }
        Map<Long,Menu> map = menus.stream()
                .collect(Collectors.toMap(Menu::getId,menu->menu));
        List<Menu> result = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParentId() == null){
                result.add(menu);
            }else {
                Menu pmenu = map.get(menu.getParentId());
                pmenu.getChildren().add(menu);
            }
        }
        return result;
    }
}
